<?php
namespace App\Http\Controllers\Admin;

use App\AppChannel;
use App\BlackAccessLog;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class StatisticsController extends Controller
{
    /**
     * @Desc: 黑名单访问列表统计
     * @Author: hank <maokeyang1031@gmail.com>
     * @param Request $request
     * @return \Illuminate\View\View
     */
    public function byDate(Request $request)
    {
        $accessDate = $request->input('access_date', '');
        $appId = $request->input('app_id');
        $channelId = $request->input('channel_id', 0);


        $mdl = BlackAccessLog::select(DB::raw("count(*) as total, access_date"))
                                    ->where("app_id", $appId);

        if (!empty($accessDate)) {
            $mdl = $mdl->where("access_date", $accessDate);
        }

        if (!empty($channelId)) {
            $mdl = $mdl->where("channel_id", $channelId);
        }


        $list = $mdl->groupBy("access_date")->orderBy("access_date", "desc")->paginate(10);

        $channels = AppChannel::where("app_id", $appId)->get(["id", "name"]);

        return view('admin.statistics.index', ['list' => $list, 'accessDate' => $accessDate,
                                        'channels' => $channels,
                                        'channelId' => $channelId, 'appId' => $appId]);
    }
}
